package cn.stronglink.asset.module.patrol.service;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
import com.baomidou.mybatisplus.plugins.Page;
import com.baomidou.mybatisplus.toolkit.IdWorker;
import cn.stronglink.asset.model.Patrol;
import cn.stronglink.asset.module.patrol.mapper.PatrolMapper;
import cn.stronglink.suite.core.util.WebUtil;

@Service
@Transactional(rollbackFor=Exception.class) 
public class PatrolService {

	@Autowired
	private PatrolMapper patrolMapper;	

	@Transactional(propagation=Propagation.NOT_SUPPORTED,readOnly=true)
	public Page<Patrol> queryList(Page<Patrol> page, Map<String, Object> params) {
		List<Patrol> list =patrolMapper.queryList(page,params);
		page.setRecords(list);
		return page;	
	}

	/***
	 * 新增
	 * @param inspection
	 * @return
	 */
	public int create(Patrol inspection) {
		inspection.setId(IdWorker.getId());
		inspection.setStatus(1);
		inspection.setType(2);
		inspection.setCreateBy(WebUtil.getCurrentUser());
		inspection.setCreateTime(new Date());
		int count =patrolMapper.insertInspection(inspection);	
		return count;
		
	}


	/***
	 * 更新  
	 * @param inspection
	 */
	public void edit(Patrol inspection) {
		inspection.setUpdateTime(new Date());
		inspection.setUpdateBy(WebUtil.getCurrentUser());
		//更新巡检
		patrolMapper.updateInspection(inspection);
	}

	/***
	 * 删除
	 * @param id
	 */
	public void remove(Long id) {
		patrolMapper.remove(id);
	}
	
	@Transactional(propagation=Propagation.NOT_SUPPORTED,readOnly=true)
	public Patrol qryById(Long id) {
		return patrolMapper.qryById(id);
	}
	
	@Transactional(propagation=Propagation.NOT_SUPPORTED,readOnly=true)
	public Page<Patrol> qryPatrolList(Page<Patrol> page, Map<String, Object> params) {
		List<Patrol> list = patrolMapper.qryPatrolList(page,params);
		page.setRecords(list);
		return page;
	}
	
	/**
	 * 查询详情列表
	 */
	@Transactional(propagation=Propagation.NOT_SUPPORTED,readOnly=true)
	public Page<HashMap> queryPatrolItemsList(Page<HashMap> page, Map<String, Object> params) {
		List<HashMap> detailsList = patrolMapper.queryPatrolItemsList(page, params);
		page.setRecords(detailsList);
		return page;
	}
}
